//一般直接写在一个js文件中
layui.use(['element','form','upload'],function () {
    var form = layui.form;
    var upload = layui.upload;
    form.on('select(itemsType)', function(data){
        var type = data.value;
        if(type == 2){
            if($('.healthDataItems').is(":empty") || !$('.healthDataItems').html().trim()){
                //获取 健康数据项
                $.ajax({
                    url:"/items/healthDataItems",
                    type:'get',
                    dataType:'json',
                    async:false,
                    success:function(res){
                        if(res.code == 200){
                            var dataMap = res.data;
                            $.each(dataMap,function(i,v){
                                var $hdib = $('<div class="healthDataItemsBox"></div>');
                                $hdib.append('<p class="leibie">'+ i +'</p>');
                                $.each(v,function (i,v) {
                                    $hdib.append('<input type="checkbox" name="healthDataItemId" value="'+v.id+'" title="'+v.name+'">');
                                });
                                $(".healthDataItems").append($hdib);
                            });
                            form.render();
                        }
                    }
                });
            }
            $("#hdibox").show();
        }else {
            $("#hdibox").hide();
        }
    });

    //上传图片
    upload.render({
        elem: '#img1'
        , url: '/storage/files/ul'
        , field: 'file'
        , accept: "images"
        , acceptMime: "image/jpg,image/jpeg,image/png"
        , size: 5120
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            var that = this;
            obj.preview(function (index, file, result) {
                $(that.elem).attr('src', result); //图片链接（base64）
            });
        }
        , done: function (res) {
            //如果上传失败
            if (res.code != 200) {
                return layer.msg('上传失败');
            }
            //上传成功
            var $target = $(this.elem);
            $target.parent().find(".hiddenIpt").val(res.data.path);
        }
        , error: function () {
            //演示失败状态
            var $target = $(this.elem);
            var demoText = $target.parent().find(".msg-box")
            demoText.html('<span style="color: #FF5722;">上传失败!</span>');
        }
    });

});